<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="菜品名称" prop="dishesName">
        <el-input
          v-model="queryParams.dishesName"
          placeholder="请输入菜品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属类别" prop="categoryId">
        <el-select v-model="queryParams.categoryId" placeholder="请选择菜品类别" clearable>
          <el-option
            v-for="item in this.categoryList"
            :key="item.categoryId"
            :label="item.categoryName"
            :value="item.categoryId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="菜品状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择菜品状态" clearable>
          <el-option
            v-for="dict in dict.type.dishes_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['rms:dishes:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['rms:dishes:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['rms:dishes:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['rms:dishes:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dishesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="菜品名称" align="center" prop="dishesName" />
      <el-table-column label="所属类别" align="center" prop="categoryName" />
      <el-table-column label="菜品单价" align="center" prop="price" />
      <el-table-column label="菜品数量" align="center" prop="account" />

<!--      <el-table-column-->
<!--        label="菜品图片"-->
<!--        align="center"-->
<!--        class-name="small-padding fixed-width"-->
<!--      >-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="small"-->
<!--            icon="el-icon-download"-->
<!--            @click="handleDownload(scope.row.pictureUrl)"-->
<!--            v-hasPermi="['rms:dishes:list']"-->
<!--            v-if="scope.row.pictureUrl != null"-->
<!--          >下载</el-button-->
<!--          >-->
<!--          <div v-if="scope.row.pictureUrl === null">无</div>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column prop="pictureUrl" label="菜品图片" align="center">
        <template slot-scope="scope">
          <img :src=" 'http://localhost:8091/profile/upload' + scope.row.pictureUrl" min-width="70" height="70" alt="菜品图片"/>
        </template>
      </el-table-column>
      <el-table-column label="菜品状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dishes_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['rms:dishes:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['rms:dishes:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改菜品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="菜品名称" prop="dishesName">
          <el-input v-model="form.dishesName" placeholder="请输入菜品名称" />
        </el-form-item>
        <el-form-item label="所属类别" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="请选择菜品类别" clearable>
            <el-option
              v-for="item in this.categoryList"
              :key="item.categoryId"
              :label="item.categoryName"
              :value="item.categoryId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="菜品单价" prop="price">
          <el-input-number v-model="form.price" placeholder="请输入菜品单价" :min="0"/>
        </el-form-item>
        <el-form-item label="菜品数量" prop="account">
          <el-input-number v-model="form.account" placeholder="请输入菜品数量" :min="1"/>
        </el-form-item>
        <el-form-item label="菜品图片" prop="pictureUrl">
          <el-upload
            ref="upload"
            :limit="1"
            accept=".jpg, .png"
            :action="upload.url"
            :headers="upload.headers"
            :file-list="upload.fileList"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
            :auto-upload="false">
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
            <el-button style="margin-left: 10px;" size="small" type="success" :loading="upload.isUploading" @click="submitUpload">上传到服务器</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="菜品状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.dishes_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDishes, getDishes, delDishes, addDishes, updateDishes } from "@/api/rms/dishes";
import { allData } from "@/api/rms/category";
import downloadFile from "@/plugins/download";
import { getToken } from "@/utils/auth";

export default {
  name: "Dishes",
  dicts: ['dishes_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 菜品信息表格数据
      dishesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dishesName: null,
        categoryId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dishesName: [
          { required: true, message: "菜品名称不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "所属类别不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "菜品单价不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "菜品状态不能为空", trigger: "change" }
        ],
      },
      // 类别信息
      categoryList: [],
      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload",
        // 上传的文件列表
        fileList: []
      },
    };
  },
  created() {
    this.getList();
    this.getCategoryList();
  },
  methods: {
    /** 查询菜品信息列表 */
    getList() {
      this.loading = true;
      listDishes(this.queryParams).then(response => {
        this.dishesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询类别信息 */
    getCategoryList() {
      allData(null).then(response => {
        this.categoryList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dishesId: null,
        dishesName: null,
        categoryId: null,
        price: null,
        pictureUrl: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        account: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dishesId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加菜品信息";
      this.upload.fileList = [];
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dishesId = row.dishesId || this.ids
      getDishes(dishesId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改菜品信息";
      });
      this.upload.fileList = [{ name: this.form.fileName, url: this.form.filePath }];
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dishesId != null) {
            updateDishes(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDishes(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dishesIds = row.dishesId || this.ids;
      this.$modal.confirm('是否确认删除菜品信息编号为"' + dishesIds + '"的数据项？').then(function() {
        return delDishes(dishesIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('rms/dishes/export', {
        ...this.queryParams
      }, `dishes_${new Date().getTime()}.xlsx`)
    },
    /** 附件下载 */
    handleDownload(filePath) {
      downloadFile.name(filePath, false);
    },
    // 文件提交处理
    submitUpload() {
      this.$refs.upload.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.isUploading = false;
      this.form.pictureUrl = response.fileName;
      this.msgSuccess(response.msg);
    }
  }
};
</script>
